using QxFramework.Core;
using QxFramework.Utilities;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BagManager : MonoSingleton<BagManager>
{
    public Item[] Bag;
    public Item[] Weapon;
    public Item[] Prop;
    public Item[] Shop;

    private PlayerControl _playerControl;
    void Start()
    {
        Bag = new Item[16];
        Weapon = new Item[4];
        Prop = new Item[4];
        Shop = new Item[6];
        _playerControl = GameMgr.Get<IPlayerManager>().Player;
        RefreshWeapon();
        RefreshProp();
        AddInBag(_playerControl.AllWeapon[0]);
        /*foreach(var v in _playerControl.AllWeapon)
        {
            AddInBag(v);
        }*/
        Weapon[0] = Bag[0];
    }

    public void CreatNextShop()
    {
        List<Item> allItem;
        allItem = new List<Item>();
        for(int i = 0; i < 7; i++)
        {
            if (_playerControl.AllWeapon[i] == null) break;
            allItem.Add(_playerControl.AllWeapon[i]);
        }
        for(int i = 0; i < 4; i++)
        {
            if (_playerControl.AllProp[i] == null) break;
            allItem.Add(_playerControl.AllProp[i]);
        }
        for(int i = 0; i < 16; i++)
        {
            if(Bag[i]!=null)
            {
                if(Bag[i] is Weapon)
                {
                    allItem.Remove(Bag[i]);
                }
            }
        }
        for (int i = 0; i < 6; i++)
        {
            int j = Random.Range(0, allItem.Count);
            Shop[i] = allItem[j];
            allItem.Remove(allItem[j]);
            if (allItem.Count == 0) return;
        }
    }
    public void RefreshWeapon()
    {
        Weapon = _playerControl.weapon;
    }
    public void RefreshProp()
    {
        Prop = _playerControl.prop;
    }
    public bool AddInBag(Item item)
    {
        for (int i = 0; i < 16; i++)
        {
            if (Bag[i] == null)
            { 
                Bag[i] = item;
                return true;
            }
        }
        return false;
    }
    public void RemoveFromBag(int idOfCell)
    {
        Bag[idOfCell] = null;
        UIManager.Instance.GetUI("BagMenu").GetComponent<BagMenu>().RemoveFromBagMenu(idOfCell);
    }

    void Update()
    {
        if (Weapon != _playerControl.weapon) Weapon = _playerControl.weapon;
    }
}
